package org.musicParser.behaviorTests;

import org.apache.commons.io.FileUtils;
import org.jbehave.core.annotations.Aliases;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.junit.Assert.*;
import java.io.File;

import static org.junit.Assert.assertEquals;

public class MusicUiSteps {

    private static WebDriver driver;

    @Given("$hostport is opened")
    public void openPage(String hostPort) throws Exception{
        String operationSystem = System.getProperty("os.name");
        System.out.println("OS.Name " + operationSystem);
        if (operationSystem.startsWith("Windows")){
            System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
        }   else if(operationSystem.startsWith("Linux")){
            System.setProperty("webdriver.chrome.driver", "chromedriver");
        }
        driver = new ChromeDriver();
        driver.get(hostPort);
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("scr_main_page.png"));
    }

    @When("$className is visible")
    public void checkClassNameIsVisible(String className){
        WebElement classObject = driver.findElement(By.className(className));
        assertTrue("Object visible with class " + className, classObject.isDisplayed());
    }

    @Then("there is \"$tagName\" tag with $contentText text is there")
    @Aliases(values={"there is \"$tagName\" with $contentText text is there"})
    public void checkTagText(String tagName, String contextText){
        WebElement mainContent = driver.findElement(By.tagName(tagName));
        assertEquals("Checking tag" + tagName, contextText, mainContent.getText());
    }

    @Then("user successfully leaves the page")
    @Aliases(values={"user quits", "user closes page"})
    public void exit(){
        driver.quit();
    }

}